package com.lzxuni.modules.plantingprogram.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgram;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgramDetail;
import com.lzxuni.modules.plantingprogram.mapper.PlantingProgramMapper;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramDetailService;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.lzxuni.common.utils.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * <p>
 * 种植方案_信息 服务实现类
 * </p>
 *
 * @author LaoNa
 * @since 2020-12-18
 */
@Service
public class PlantingProgramServiceImpl extends ServiceImpl<PlantingProgramMapper, PlantingProgram> implements PlantingProgramService {


    @Autowired
    private PlantingProgramDetailService plantingProgramDetailService;

    @Override
    public PageInfo<PlantingProgram> queryPage(PageParameter pageParameter, PlantingProgram plantingProgram) {
        if (StringUtils.isNotEmpty(pageParameter.getSidx())) {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                    pageParameter.getSidx() + " " + pageParameter.getSord());
        } else {
            PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows());
        }
        List<PlantingProgram> plantingProgramList = baseMapper.queryList(plantingProgram);
        PageInfo<PlantingProgram> pageInfo = new PageInfo<>(plantingProgramList);
        return pageInfo;
    }

    @Override
    public List<PlantingProgram> queryList(PlantingProgram plantingProgram) {
        List<PlantingProgram> plantingProgramList = baseMapper.queryList(plantingProgram);
        return plantingProgramList;
    }


    @Override
    public boolean copy() {
        int lastYest = Integer.parseInt(DateFormatUtils.format(new Date(), "yyyy")) - 1;
        List<PlantingProgram> plantingProgramList = baseMapper.selectList(new QueryWrapper<PlantingProgram>().eq("program_year", lastYest));
        if(plantingProgramList!=null&&plantingProgramList.size()>0) {
            for (PlantingProgram plantingProgram : plantingProgramList) {
                String id = UuidUtil.get32UUID();
                List<PlantingProgramDetail> plantingProgramDetailList = plantingProgramDetailService.list(new QueryWrapper<PlantingProgramDetail>().eq("program_id", plantingProgram.getProgramId()));
                if(plantingProgramDetailList!=null&&plantingProgramDetailList.size()>0) {
                    for (PlantingProgramDetail plantingProgramDetail : plantingProgramDetailList) {
                        plantingProgramDetail.setProgramId(id);
                        plantingProgramDetail.setCreateDate(null);
                        plantingProgramDetail.setJobDate(DateUtils.addYears(plantingProgramDetail.getJobDate(),1));
                        plantingProgramDetail.setProgramId(id);
                        plantingProgramDetail.setDetailId(null);

                    }
                    plantingProgramDetailService.saveBatch(plantingProgramDetailList);
                }
                plantingProgram.setCreateDate(null);
                plantingProgram.setProgramId(id);
                plantingProgram.setProgramYear(DateFormatUtils.format(new Date(), "yyyy"));
            }
            this.saveBatch(plantingProgramList);
        }

        return true;
    }

}
